package com.atguigu.stack;

public class StackLinkedList {
    private int size;
    private Node head;//头节点

    public StackLinkedList() {
        size = 0;
        head = null;
    }

    class Node {
        public Object data;// 每个节点的数据
        public Node next;// 每个节点指向下一个节点的连接

        public Node(Object data) {
            this.data = data;
        }
    }


    public void insertHead(Object value) {
        Node newNode = new Node(value);
        if (size == 0) {
            head = newNode;
        } else {
            newNode.next = head;
            head = newNode;
        }
        size++;
    }

    public Node delectHead() {
        if (size > 0) {
            Node oldHead = head;
            head = head.next;
            size--;
            return oldHead;
        } else {
            return null;
        }
    }

    //判断栈内元素是否为空
    public boolean isNull() {
        return size == 0;
    }
//    打印
    public void printNode() {
        if (size > 0) {
            Node node = head;
            if (size == 1) {
                System.out.println("["+node.data+"]");
                return;
            }
            int item = size;
            System.out.print("[");
            while (item > 0) {
                if (node.next == null) {
                    System.out.print(node.data + "]");
                }else {
                    System.out.print(node.data + "->");
                }
                node = node.next;
                item--;
            }
        }else {
            System.out.println("[]");
        }
    }
//打印
//   public void printNode(){
//       Node node = head;
//        while (true){
//            System.out.print(node.data+"->");
//            if (node.next==null){
//                break;
//            }
//            node=node.next;
//        }
//}

    public static void main(String[] args) {
        StackLinkedList stackLinkedList = new StackLinkedList();
        stackLinkedList.insertHead(1);
        stackLinkedList.insertHead(1333);
        stackLinkedList.insertHead(1555);
        System.out.println("入栈之后的结果");
        stackLinkedList.printNode();
        System.out.println();
        System.out.println("出栈之后的结果");
        stackLinkedList.delectHead();
        stackLinkedList.printNode();
    }
}